<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('销账')"/>
    <th:block th:include="include :: datetimepicker-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-cancel-add" >
        <input id="incomingId" name="incomingId" th:value="${incomingId}" type="hidden">
        <input id="expenses" name="params[expenses]" type="hidden">
        <input id="taxs" name="params[taxs]" type="hidden">
        <input id="invoices" name="params[invoices]" type="hidden">
        <input id="papers" name="params[papers]" type="hidden" th:value="${papers}">
    </form>

    <div class="row">
        <div class="col-sm-12  table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
    <div class="row">
        <button type="button" class="btn btn-primary" onclick="selectExpense(2)">添加返款单</button>
        <div class="col-sm-12 table-striped">
            <table id="backpay-table"></table>
        </div>
    </div>
    <div class="row">
        <button type="button" class="btn btn-primary" onclick="selectExpense(3)">添加税金报销单</button>
        <div class="col-sm-12 table-striped">
            <table id="tax-table"></table>
        </div>
    </div>
    <div class="row">
        <button type="button" class="btn btn-primary" onclick="selectInvoice()">关联发票</button>
        <button type="button" class="btn btn-primary" onclick="selectPaper()">销部分稿件</button>
        <div class="col-sm-12 table-striped">
            <table id="invoice-table"></table>
        </div>
    </div>
</div>


<th:block th:include="include :: footer"/>
<th:block th:include="include :: datetimepicker-js"/>
<script th:inline="javascript">
    var bankNoDatas = [[${@dict.getType('work_bank')}]];
    var invoiceTypeDatas = [[${@dict.getType('work_invoice_type')}]];
    var invoiceCompanyDatas = [[${@dict.getType('work_invoice_company')}]];
    var isContractDatas = [[${@dict.getType('work_yes_no')}]];
    var expenseTypeDatas = [[${@dict.getType('work_expense')}]];
    var expenseStatusDatas = [[${@dict.getType('work_check_status')}]];
    var payTypeDatas = [[${@dict.getType('work_out_type')}]];
    var prefix = ctx + "work/incoming"
    $("#form-incoming-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {

            var backpay = $("#backpay-table").bootstrapTable('getData');
            $("#expenses").val(backpay.map(e => e.expenseId));
            var tax = $("#tax-table").bootstrapTable('getData');
            $("#taxs").val(tax.map(e => e.expenseId));
            var invoice = $("#invoice-table").bootstrapTable('getData');
            $("#invoices").val(invoice.map(e => e.invoiceId));

            $.operate.save(prefix + "/addcancel/2", $('#form-cancel-add').serialize());
        }
    }

    $("input[name='incomingDate']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    $(function () {
        var options = {
            url: prefix + "/cancellist/" + $("#incomingId").val(),
            id: "bootstrap-table",
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            pagination: false,
            columns: [
                [{
                    title: '你选择的收款认领信息',
                    align: 'left',
                    colspan: 10
                }
                ],
                [
                    {
                        field: 'incomingId',
                        title: '认领ID',
                        visible: false
                    },
                    {
                        field: 'incomingName',
                        title: '来款人'
                    },
                    {
                        field: 'bankName',
                        title: '来款银行',
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(bankNoDatas, value);
                        }
                    },
                    {
                        field: 'bankAccount',
                        title: '来款账号'
                    },
                    {
                        field: 'money',
                        title: '来款额度'
                    },
                    {
                        field: 'refund',
                        title: '返款金额'
                    },
                    {
                        field: 'taxes',
                        title: '税金'
                    },
                    {
                        field: 'expenseLimit',
                        title: '已销额度'
                    },
                    {
                        field: 'incomingDate',
                        title: '来款日期'
                    },
                    {
                        field: 'remark',
                        title: '备注'
                    }]]
        };
        $.table.init(options);
        var options1 = {
            url: ctx + "work/expense/backpay/" + $("#incomingId").val(),
            id: "backpay-table",
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            pagination: false,
            unique: "expenseId",
            columns: [
                [{
                    title: "关联的返款单",
                    align: "left",
                    colspan: 10
                }
                ],
                [
                    {
                        field: 'expenseId',
                        title: '报销ID'
                    },
                    {
                        field: 'expenseType',
                        title: '报销类型',
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(expenseTypeDatas, value);
                        }
                    },
                    {
                        field: 'expenseFee',
                        title: '报销费用'
                    },
                    {
                        field: 'expenseStatus',
                        title: '审核状态',
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(expenseStatusDatas, value);
                        }
                    },
                    {
                        field: 'payType',
                        title: '支出类型',
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(payTypeDatas, value);
                        }
                    },
                    {
                        field: 'bankName',
                        title: '银行名称',
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(bankNoDatas, value);
                        }
                    },
                    {
                        field: 'bankAccount',
                        title: '银行账号'
                    },

                    {
                        field: 'expenser',
                        title: '报销人'
                    },

                    {
                        field: 'remark',
                        title: '报销内容概要'
                    },{
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-danger btn-xs" href="#" onclick="removeRow(\'expenseId\',\'' + row.expenseId + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
            ]
        };
        $.table.init(options1);
        var options2 = {
            url: ctx + "work/expense/tax/" + $("#incomingId").val(),
            id: "tax-table",
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            pagination: false,
            unique: "expenseId",
            columns: [
                [{
                    title: "关联的税金报销单",
                    align: "left",
                    colspan: 10
                }
                ],
                [
                    {
                        field: 'expenseId',
                        title: '报销ID'


                    },
                    {
                        field: 'expenseType',
                        title: '报销类型',
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(expenseTypeDatas, value);
                        }
                    },
                    {
                        field: 'expenseFee',
                        title: '报销费用'
                    },
                    {
                        field: 'expenseStatus',
                        title: '审核状态',
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(expenseStatusDatas, value);
                        }
                    },
                    {
                        field: 'payType',
                        title: '支出类型',
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(payTypeDatas, value);
                        }
                    },
                    {
                        field: 'bankName',
                        title: '银行名称',
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(bankNoDatas, value);
                        }
                    },
                    {
                        field: 'bankAccount',
                        title: '银行账号'
                    },

                    {
                        field: 'expenser',
                        title: '报销人'
                    },

                    {
                        field: 'remark',
                        title: '报销内容概要'
                    },{
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-danger btn-xs" href="#" onclick="removeRow(\'expenseId\',\'' + row.expenseId + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
            ]
        };
        $.table.init(options2);
        var options3 = {
            url: ctx + "work/invoice/cancel/" + $("#incomingId").val(),
            id: "invoice-table",
            showFooter: true,
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            pagination: false,
            unique: "invoiceId",
            columns: [
                [{
                    title: "关联的发票",
                    align: "left",
                    colspan: 21
                }
                ], [
                    {
                        field: 'invoiceId',
                        title: '发票ID',
                        footerFormatter:function (value) {
                            return '合计';
                        }

                    },
                    {
                        field: 'invoicePrice',
                        title: '开票稿件合作金额',
                        footerFormatter:function (value) {
                            var sumBalance = 0;
                            for (var i in value) {
                                sumBalance += parseFloat(value[i].invoicePrice);
                            }
                            return sumBalance;
                        }
                    },
                    {
                        field: 'paperPrice',
                        title: '已选稿件金额'
                    },
                    {
                        field: 'taxPoint',
                        title: '税点'
                    },
                    {
                        field: '',
                        title: '税金',
                        formatter: function (value, row, index) {
                            return row.invoicePrice * row.taxPoint / 100;
                        },
                        footerFormatter:function (value) {
                            var sumBalance = 0;
                            for (var i in value) {
                                sumBalance += parseFloat(value[i].invoicePrice*value[i].taxPoint/100);
                            }
                            return sumBalance;
                        }

                    },
                    {
                        field: 'taxPoint',
                        title: '价税合计',
                        formatter: function (value, row, index) {
                            return row.invoicePrice + (row.invoicePrice * row.taxPoint / 100);
                        }
                    },
                    {
                        field: 'payback',
                        title: '客户还款'
                    },
                    {
                        field: 'invoiceType',
                        title: '发票类型',
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(invoiceTypeDatas, value);
                        }
                    },
                    {
                        field: 'invoiceTitle',
                        title: '发票抬头'
                    },
                    {
                        field: 'customerName',
                        title: '客户名称'
                    },
                    {
                        field: 'invoiceCompany',
                        title: '开哪个公司',
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(invoiceCompanyDatas, value);
                        }
                    },
                    {
                        field: 'invoiceContent',
                        title: '开票内容'
                    },

                    {
                        field: 'taxNo',
                        title: '税务登记证号'
                    },
                    {
                        field: 'bankNo',
                        title: '基本开户银行',
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(bankNoDatas, value);
                        }
                    },
                    {
                        field: 'bankAccount',
                        title: '开户银行账号'
                    },
                    {
                        field: 'isContract',
                        title: '合同',
                        formatter: function (value, row, index) {
                            return $.table.selectDictLabel(isContractDatas, value);
                        }
                    },
                    {
                        field: 'contractDate',
                        title: '合同收集日期'
                    },
                    {
                        field: 'registerAddress',
                        title: '注册场所地址'
                    },
                    {
                        field: 'registerTel',
                        title: '注册场所电话'
                    },
                    {
                        field: 'remark',
                        title: '其它要求'
                    },{
                        title: '操作',
                        align: 'center',
                        formatter: function(value, row, index) {
                            var actions = [];
                            actions.push('<a class="btn btn-danger btn-xs" href="#" onclick="removeRow(\'invoiceId\',\'' + row.invoiceId + '\')"><i class="fa fa-remove"></i>删除</a>');
                            return actions.join('');
                        }
                    }]]
        };
        $.table.init(options3);
    });
    function removeRow(field,id) {
        table.set();

        $('#'+table.options.id).bootstrapTable('remove', {
            field: field,
            values: id
        })

    }
    // callBack获取父窗口方法（方式二）
    function selectExpense(exType) {

        var options = {
            title: exType == '2' ? '选择返款单' : "选择税金报销单",
            url: prefix + "/expense/" + exType,
            width: 800,
            callBack: exType == '2' ? submitBack : submitTax
        };
        $.modal.openOptions(options);
    }

    function selectInvoice() {
        var options = {
            title: "选择发票",
            url: prefix + "/invoice/",
            width: 800,
            callBack: submitInvoice
        };
        $.modal.openOptions(options);
    }

    function selectPaper() {
        var data = $('#invoice-table').bootstrapTable('getData');
        if (data.length != 1) {
            $.modal.alertWarning("销部分稿件时只能选择一张发票！");
            return;
        }

        var options = {
            title: "选择稿件",
            url: prefix + "/paper/" + $("#incomingId").val() + "/" + data[0].invoiceId,
            width: 800,
            callBack: submitPaper
        };
        $.modal.openOptions(options);
    }

    function submitBack(index, layero) {
        var rows = layero.find("iframe")[0].contentWindow.getSelections();
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一条记录");
            return;
        }
        $("#backpay-table").bootstrapTable('append', rows);
        $.modal.close(index);
    }

    function submitTax(index, layero) {
        var rows = layero.find("iframe")[0].contentWindow.getSelections();
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一条记录");
            return;
        }
        $("#tax-table").bootstrapTable('append', rows);
        $.modal.close(index);
    }

    function submitInvoice(index, layero) {
        var rows = layero.find("iframe")[0].contentWindow.getSelections();
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一条记录");
            return;
        }

        //alert('我是通过方式二来的：' + rows.join())
        $("#invoice-table").bootstrapTable('append', rows);
        $.modal.close(index);
    }

    function submitPaper(index, layero) {

        var rows = layero.find("iframe")[0].contentWindow.getSelections();
/*        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一条记录");
            return;
        }*/
        //alert('我是通过方式二来的：' + rows.join())
        $('#papers').val(rows);
        $.modal.close(index);
    }
</script>
</body>
</html>